<?php

namespace App\Http\Controllers\admin;
use DB;
use Config;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Requests\bannerinsRequest;
use App\Http\Requests\bannereditRequest;
use App\Http\Controllers\Controller;

class BannerController extends Controller
{
    /**
    *banner添加
    * /admin/banner/add  添加页面
    * /admin/banner/insert 插入数据
    */

    public function getAdd() {   //跳转到添加页面
        return view("banner.add");
    }
    public function postInsert(bannerinsRequest $request) {  //添加
        // dd($request->all());
        // 执行上传
        if ($request->hasFile("pic")) {
            $path = config::get("app.upload_path"); //获取文件上传的路径
            
            //给图片重命名
            do {
                $picname = date("YmdHis").rand(10000,9999).$request->file('pic')->getClientOriginalName();
            } while (file_exists($path.$picname));
            //插入到数据库
            $data = $request -> except(["_token","pic"]);
            //注入pic元素
            $data["pic"] = $picname;

            //插入到数据库
            $res = DB::table("banner") -> insert($data);
       
            //解析结果集
            if ($res) {

                //执行上传  
                $request->file('pic') -> move($path,$picname); 

                //设置图片缩放必要参数
                $imgDir = $path.$picname;

                //缩放图片
                imgR($imgDir,800,480,"m_");
                imgR($imgDir,50,50,"s_");
                
                //页面跳转并提示
                return redirect("/admin/banner/index") -> with("success","上传成功！");
            } else {

                //返回并提示
                return back() -> with("error","上传失败!");
            }
        } 
    }

    /**
    *banner浏览
    * /admin/banner/index  浏览页面
    */
    public function getIndex(Request $request){

        //获取图片上传路径
        $path = trim(config::get("app.upload_path"),".");

        //查询数据
        $res = DB::table("banner") -> where(function($query)use($request) {
            
            //判断是否存在搜索
            if ($request -> input("keywords")) {
                $query -> where("order", $request -> input("keywords"))
                -> orWhere("id", $request -> input("keywords"));
            } 
        }) -> paginate($request -> input("num",10));
        return view("banner.index",["list" => $res, "path" => $path, "request" => $request -> all()]);
    }

    /**
    *banner删除
    * /admin/banner/delete  浏览页面
    */
    public function getDelete($id,$pic) {
        
        //获取图片上传路径
        $path = config::get("app.upload_path");
        
        //删除数据库内容
        $res = DB::table("banner") -> where("id", $id) -> delete();

        //解析结果集
        if ($res) {

            //成功则删除图片
            unlink($path.$pic);
            unlink($path."s_".$pic);
            unlink($path."m_".$pic);


            //跳转并告知用户
            return redirect("/admin/banner/index") -> with("success", "删除成功！");
        } else {

            //失败则返回
            return back() -> with("error", "删除失败！");
        }
    }

    /**
    *banner排序号修改
    * /admin/banner/edit  编辑页面
    * /admin/banner/update 执行编辑
    */
    //编辑页面
    public function getEdit($id) {

        //获取图片上传路径
        $path = ltrim(config::get("app.upload_path"),".");
        
        //查询原有信息
        $res = DB::table("banner") -> select("id","pic","order") -> where("id",$id) -> first(); 
        
        //跳转到编辑页面
        return view("banner.edit", ["v" => $res, "path" => $path]);
    }

    //修改
    public function postUpdate(bannereditRequest $request) {
        
        //修改数据库
        $res = DB::table("banner") -> where("id",$request -> input("id")) -> update(["order" => $request -> input("order")]);
        
        //解析结果集
        if ($res){

            //返回并提示
            return redirect("/admin/banner/index") -> with("success", "修改成功！");
        } else {
            return back() -> with("error", "修改失败！");
        }
    }

    /**
    *banner是否前台显示修改
    * /admin/banner/delete  浏览页面
    */
    public function getStatus($status,$id) {
        
        //根据当前状态确定修改过后的状态
        if($status == 1) {
            $num = 0;
        } else {
            $num = 1;
        }
        
        //执行修改
        $res = DB::table("banner") -> where("id", $id) -> update(["status" => $num]);

        //解析结果集
        if ($res){

            //返回并提示
            return redirect("/admin/banner/index") -> with("success", "修改成功！");
        } else {
            return back() -> with("error", "修改失败！");
        }
    }
}
